using LocalBoard.Domain;
using LocalBoard.Persistence;

using NUnit.Framework;

namespace LocalBoard.Controller
{
	[TestFixture]
	public class TestClubSecurity
	{
		private ClubSecurity _publicClubSecurity;
		private ClubSecurity _privateClubSecurity;
		private ClubSecurity _hiddenClubSecurity;
		private IAccessorFactory _accessorFactory;
		private Profile _publicClubManager;
		private Profile _privateClubManager;
		private Profile _publicClubUser;
		private Profile _admin;
		private Profile _anonymous;
		private Profile _hiddenClubManager;
		private Profile _hiddenClubUser;
		private Profile _privateClubUser;

		[SetUp]
		public void setUp()
		{
			_accessorFactory = new MockAccessorFactory();
			AdminController adminController = new AdminController(_accessorFactory);

			//Public club
			int publicClubId = adminController.CreateClub(new Club("test club", "test club", 1, "test club",ClubType.Public, null));
			int publicClubManagerId = adminController.CreateUser(new Profile("publicClubManagerId", "userName 1", "password1", "email1@mail.com"));
			ClubController publicClubController = new ClubController(publicClubId, _accessorFactory);
			publicClubController.AssignClubManager(publicClubManagerId);
			_publicClubManager = adminController.GetUser(publicClubManagerId);
			int publicClubUserId = adminController.CreateUser(new Profile("publicClubUserId", "userName 2", "password1", "email1@mail.com"));
			publicClubController.AddUserClubProfile(null, publicClubUserId);
			_publicClubUser = adminController.GetUser(publicClubUserId);
			_publicClubSecurity = publicClubController.GetClubSecurity();

			//Private club
			int privateClubId = adminController.CreateClub(new Club("test club2", "test club 2", 1, "test club 2",ClubType.Private, null));
			int privateClubManagerId = adminController.CreateUser(new Profile("privateClubManagerId", "userName 2", "password1", "email2@mail.com"));
			ClubController privateClubController = new ClubController(privateClubId, _accessorFactory);
			privateClubController.AssignClubManager(privateClubManagerId);
			_privateClubManager = adminController.GetUser(privateClubManagerId);
			int privateClubUserId = adminController.CreateUser(new Profile("privateClubUserId", "userName 2", "password1", "email1@mail.com"));
			privateClubController.AddUserClubProfile(null, privateClubUserId);
			_privateClubUser = adminController.GetUser(privateClubUserId);
			_privateClubSecurity = privateClubController.GetClubSecurity();

			//Hidden club
			int hiddenClubId = adminController.CreateClub(new Club("test club3", "test club 2", 1, "test club 2",ClubType.Hidden, null));
			int hiddenClubManagerId = adminController.CreateUser(new Profile("hiddenClubManagerId", "userName 1", "password1", "email1@mail.com"));
			ClubController hiddenClubController = new ClubController(hiddenClubId, _accessorFactory);
			hiddenClubController.AssignClubManager(hiddenClubManagerId);
			_hiddenClubManager = adminController.GetUser(hiddenClubManagerId);
			int hiddenClubUserId = adminController.CreateUser(new Profile("hiddenClubUserId", "userName 2", "password1", "email1@mail.com"));
			hiddenClubController.AddUserClubProfile(null, hiddenClubUserId);
			_hiddenClubUser = adminController.GetUser(hiddenClubUserId);
			_hiddenClubSecurity = hiddenClubController.GetClubSecurity();
			
			int adminId = adminController.CreateUser(new Profile("admin", "admin", "password1", "email2@mail.com"));
			adminController.AssignUserRoleToUser(adminController.GetUserRole(UserRole.ADMIN_ROLE_NAME).Id, adminId);
			_admin = adminController.GetUser(adminId);

			_anonymous = adminController.GetAnonymousUser();
		}

		[TearDown]
		public void tearDown()
		{
			MockAccessorFactory.ResetDataTable();
			_publicClubSecurity = null;
			_privateClubSecurity = null;
			_accessorFactory = null;
			_publicClubManager = null;
			_privateClubManager = null;
			_publicClubUser = null;
			_admin = null;
		}

		[Test]
		public void testCanUserSeeClubListing()
		{
			Assert.IsTrue(_publicClubSecurity.CanUserSeeClubListing(_admin));
			Assert.IsTrue(_publicClubSecurity.CanUserSeeClubListing(_anonymous));
			Assert.IsTrue(_publicClubSecurity.CanUserSeeClubListing(_publicClubUser));
			Assert.IsTrue(_publicClubSecurity.CanUserSeeClubListing(_privateClubUser));
			Assert.IsTrue(_publicClubSecurity.CanUserSeeClubListing(_hiddenClubUser));

			Assert.IsTrue(_privateClubSecurity.CanUserSeeClubListing(_admin));
			Assert.IsTrue(_privateClubSecurity.CanUserSeeClubListing(_anonymous));
			Assert.IsTrue(_privateClubSecurity.CanUserSeeClubListing(_publicClubUser));
			Assert.IsTrue(_privateClubSecurity.CanUserSeeClubListing(_privateClubUser));
			Assert.IsTrue(_privateClubSecurity.CanUserSeeClubListing(_hiddenClubUser));

			Assert.IsTrue(_hiddenClubSecurity.CanUserSeeClubListing(_admin));
			Assert.IsFalse(_hiddenClubSecurity.CanUserSeeClubListing(_anonymous));
			Assert.IsFalse(_hiddenClubSecurity.CanUserSeeClubListing(_publicClubUser));
			Assert.IsFalse(_hiddenClubSecurity.CanUserSeeClubListing(_privateClubUser));
			Assert.IsTrue(_hiddenClubSecurity.CanUserSeeClubListing(_hiddenClubUser));
		}
		[Test]
		public void testCanUserAccessClub()
		{
			Assert.IsTrue(_publicClubSecurity.CanUserAccessClub(_admin));
			Assert.IsTrue(_publicClubSecurity.CanUserAccessClub(_anonymous));
			Assert.IsTrue(_publicClubSecurity.CanUserAccessClub(_publicClubUser));
			Assert.IsTrue(_publicClubSecurity.CanUserAccessClub(_privateClubUser));
			Assert.IsTrue(_publicClubSecurity.CanUserAccessClub(_hiddenClubUser));

			Assert.IsTrue(_privateClubSecurity.CanUserAccessClub(_admin));
			Assert.IsFalse(_privateClubSecurity.CanUserAccessClub(_anonymous));
			Assert.IsFalse(_privateClubSecurity.CanUserAccessClub(_publicClubUser));
			Assert.IsTrue(_privateClubSecurity.CanUserAccessClub(_privateClubUser));
			Assert.IsFalse(_privateClubSecurity.CanUserAccessClub(_hiddenClubUser));

			Assert.IsTrue(_hiddenClubSecurity.CanUserAccessClub(_admin));
			Assert.IsFalse(_hiddenClubSecurity.CanUserAccessClub(_anonymous));
			Assert.IsFalse(_hiddenClubSecurity.CanUserAccessClub(_publicClubUser));
			Assert.IsFalse(_hiddenClubSecurity.CanUserAccessClub(_privateClubUser));
			Assert.IsTrue(_hiddenClubSecurity.CanUserAccessClub(_hiddenClubUser));
		}
		[Test]
		public void testCanUserCreateBoard()
		{
			Assert.IsTrue(_publicClubSecurity.CanUserCreateBoard(_publicClubManager));
			Assert.IsFalse(_publicClubSecurity.CanUserCreateBoard(_publicClubUser));
			Assert.IsFalse(_publicClubSecurity.CanUserCreateBoard(_privateClubManager));
			Assert.IsFalse(_privateClubSecurity.CanUserCreateBoard(_publicClubManager));
			Assert.IsFalse(_privateClubSecurity.CanUserCreateBoard(_publicClubUser));
			Assert.IsTrue(_privateClubSecurity.CanUserCreateBoard(_privateClubManager));
			Assert.IsTrue(_privateClubSecurity.CanUserCreateBoard(_admin));
		}

		[Test]
		public void testCanUserDeleteBoard()
		{
			Assert.IsTrue(_publicClubSecurity.CanUserDeleteBoard(_publicClubManager));
			Assert.IsFalse(_publicClubSecurity.CanUserDeleteBoard(_publicClubUser));
			Assert.IsFalse(_publicClubSecurity.CanUserDeleteBoard(_privateClubManager));
			Assert.IsFalse(_privateClubSecurity.CanUserDeleteBoard(_publicClubManager));
			Assert.IsFalse(_privateClubSecurity.CanUserDeleteBoard(_publicClubUser));
			Assert.IsTrue(_privateClubSecurity.CanUserDeleteBoard(_privateClubManager));
			Assert.IsTrue(_privateClubSecurity.CanUserDeleteBoard(_admin));
		}

		[Test]
		public void testCanUserUpdateBoard()
		{
			Assert.IsTrue(_publicClubSecurity.CanUserUpdateBoard(_publicClubManager));
			Assert.IsFalse(_publicClubSecurity.CanUserUpdateBoard(_publicClubUser));
			Assert.IsFalse(_publicClubSecurity.CanUserUpdateBoard(_privateClubManager));
			Assert.IsFalse(_privateClubSecurity.CanUserUpdateBoard(_publicClubManager));
			Assert.IsFalse(_privateClubSecurity.CanUserUpdateBoard(_publicClubUser));
			Assert.IsTrue(_privateClubSecurity.CanUserUpdateBoard(_privateClubManager));
			Assert.IsTrue(_privateClubSecurity.CanUserUpdateBoard(_admin));
		}

		[Test]
		public void testCanUserManageClub()
		{
			Assert.IsTrue(_publicClubSecurity.CanUserManageClub(_publicClubManager));
			Assert.IsFalse(_publicClubSecurity.CanUserManageClub(_publicClubUser));
			Assert.IsFalse(_publicClubSecurity.CanUserManageClub(_privateClubManager));
			Assert.IsTrue(_publicClubSecurity.CanUserManageClub(_admin));
			Assert.IsFalse(_privateClubSecurity.CanUserManageClub(_publicClubManager));
			Assert.IsFalse(_privateClubSecurity.CanUserManageClub(_publicClubUser));
			Assert.IsTrue(_privateClubSecurity.CanUserManageClub(_privateClubManager));
			Assert.IsTrue(_privateClubSecurity.CanUserManageClub(_admin));
		}
	}
}